﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Accessorial : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlCostType;
        protected HtmlForm F9171;
        protected HtmlInputHidden hdnAccessorialID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlHead Head1;
        protected HtmlImage img1;
        protected HtmlImage imgOwnerID;
        protected Label lblAccessorialID;
        protected Label lblChargeInvocation;
        protected Label lblChargePerUnit;
        protected Label lblChargeUnit;
        protected Label lblCostInvocation;
        protected Label lblCostPerUnit;
        protected Label lblCostRateID;
        protected Label lblCostType;
        protected Label lblCostUnit;
        protected Label lblDescr;
        protected Label lblGeneralLedgerID;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblRateID;
        protected Label lblTaxGroupID;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAccessorialID;
        protected TextBox txtChargeInvocation;
        protected TextBox txtChargePerUnit;
        protected TextBox txtChargeUnit;
        protected TextBox txtCostInvocation;
        protected TextBox txtCostPerUnit;
        protected TextBox txtCostRateID;
        protected TextBox txtCostUnit;
        protected TextBox txtDescr;
        protected TextBox txtGeneralLedgerID;
        protected TextBox txtOwnerID;
        protected TextBox txtRateID;
        protected TextBox txtTaxGroupID;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtAccessorialID.Text = "";
            this.txtAccessorialID.Enabled = true;
            base.DataEntityViewState = null;
            this.hdnFlag.Value = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtAccessorialID.Text.Trim()))
            {
                try
                {
                    List<string> RateIDS = new List<string> {
                        this.txtAccessorialID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(RateIDS, "ACCESSORIAL") + base.GetGlobalMsgResource("F9170_009");
                    base.DataEntityViewState = null;
                    this.btnCopy.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    base.Cache.Remove("Accessorial");
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9170_010") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("AccessorialQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("AccessorialList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("Accessorial.aspx?from={0}", base.UrlEncode(this.hdnFrom.Value.Trim())), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = string.Empty;
            if (string.IsNullOrEmpty(this.txtDescr.Text))
            {
                this.lblMsg.Text = "描述不能为空";
            }
            else
            {
                if (this.hdnFlag.Value == "1")
                {
                    this.UpdateAccessorialInfo();
                }
                else
                {
                    this.InsertAccessorialInfo();
                }
                base.Cache.Remove("Accessorial");
            }
        }

        private void InsertAccessorialInfo()
        {
            try
            {
                AccessorialInfo accessorialInfo = new AccessorialInfo {
                    AccessorialID = this.txtAccessorialID.Text,
                    OwnerID = this.txtOwnerID.Text,
                    Descr = this.txtDescr.Text,
                    RateID = "",
                    TaxGroupID = this.txtTaxGroupID.Text,
                    CostRateID = this.txtCostRateID.Text,
                    GeneralLedgerID = this.txtGeneralLedgerID.Text,
                    ChargeUnit = this.txtChargeUnit.Text,
                    CostUnit = this.txtCostUnit.Text
                };
                if (!string.IsNullOrEmpty(this.txtChargePerUnit.Text))
                {
                    accessorialInfo.ChargePerUnit = decimal.Parse(this.txtChargePerUnit.Text);
                }
                if (!string.IsNullOrEmpty(this.txtCostPerUnit.Text))
                {
                    accessorialInfo.CostPerUnit = decimal.Parse(this.txtCostPerUnit.Text);
                }
                decimal tempDec = 0M;
                if (!string.IsNullOrEmpty(this.txtRateID.Text.Trim()))
                {
                    if (decimal.TryParse(this.txtRateID.Text.Trim(), out tempDec))
                    {
                        accessorialInfo.Rate = decimal.Parse(this.txtRateID.Text.Trim());
                    }
                    else
                    {
                        accessorialInfo.Rate = tempDec;
                    }
                }
                else
                {
                    accessorialInfo.Rate = tempDec;
                }
                accessorialInfo.CostType = this.ddlCostType.SelectedValue;
                accessorialInfo.ChargeInvocation = this.txtChargeInvocation.Text;
                accessorialInfo.CostInvocation = this.txtCostInvocation.Text;
                accessorialInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                accessorialInfo.CreatedBy = base.UserInfo.UserID;
                this.hdnAccessorialID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(accessorialInfo, "ACCESSORIAL");
                this.hdnLineID.Value = "10";
                this.hdnFlag.Value = "1";
                base.DataEntityViewState = accessorialInfo;
                this.txtAccessorialID.Text = this.hdnAccessorialID.Value;
                this.txtAccessorialID.Enabled = false;
                this.lblMsg.Text = this.hdnAccessorialID.Value + base.GetGlobalMsgResource("F9170_007");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9170_008") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlCostType, "CostType");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnAccessorialID.Value = base.Request.QueryString["ID"].ToString();
                this.SetRateInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetRateInfo2Form()
        {
            try
            {
                AccessorialInfo accessorialInfo = (AccessorialInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.hdnAccessorialID.Value, "ACCESSORIAL");
                if (accessorialInfo != null)
                {
                    this.txtAccessorialID.Text = accessorialInfo.AccessorialID;
                    this.txtAccessorialID.Enabled = false;
                    this.txtOwnerID.Text = accessorialInfo.OwnerID;
                    this.txtDescr.Text = accessorialInfo.Descr;
                    this.txtTaxGroupID.Text = accessorialInfo.TaxGroupID;
                    this.txtRateID.Text = base.GetPreferenceDigit(accessorialInfo.Rate);
                    this.txtCostRateID.Text = accessorialInfo.CostRateID;
                    this.txtGeneralLedgerID.Text = accessorialInfo.GeneralLedgerID;
                    this.txtChargeUnit.Text = accessorialInfo.ChargeUnit;
                    this.txtCostUnit.Text = accessorialInfo.CostUnit;
                    this.txtChargePerUnit.Text = base.GetPreferenceDigit(accessorialInfo.ChargePerUnit);
                    this.txtCostPerUnit.Text = base.GetPreferenceDigit(accessorialInfo.CostPerUnit);
                    this.txtChargeInvocation.Text = accessorialInfo.ChargeInvocation;
                    this.txtCostInvocation.Text = accessorialInfo.CostInvocation;
                    this.ddlCostType.SelectedValue = accessorialInfo.CostType;
                    this.chkIsActive.Checked = accessorialInfo.IsActive == "Y";
                    base.DataEntityViewState = accessorialInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateAccessorialInfo()
        {
            try
            {
                AccessorialInfo accessorialInfo = base.DataEntityViewState as AccessorialInfo;
                accessorialInfo.OwnerID = this.txtOwnerID.Text;
                accessorialInfo.Descr = this.txtDescr.Text;
                accessorialInfo.TaxGroupID = this.txtTaxGroupID.Text;
                accessorialInfo.RateID = "";
                accessorialInfo.CostRateID = this.txtCostRateID.Text;
                accessorialInfo.GeneralLedgerID = this.txtGeneralLedgerID.Text;
                accessorialInfo.ChargeUnit = this.txtChargeUnit.Text;
                accessorialInfo.CostUnit = this.txtCostUnit.Text;
                if (!string.IsNullOrEmpty(this.txtChargePerUnit.Text))
                {
                    accessorialInfo.ChargePerUnit = decimal.Parse(this.txtChargePerUnit.Text);
                }
                if (!string.IsNullOrEmpty(this.txtCostPerUnit.Text))
                {
                    accessorialInfo.CostPerUnit = decimal.Parse(this.txtCostPerUnit.Text);
                }
                decimal tempDec = 0M;
                if (!string.IsNullOrEmpty(this.txtRateID.Text.Trim()))
                {
                    if (decimal.TryParse(this.txtRateID.Text.Trim(), out tempDec))
                    {
                        accessorialInfo.Rate = decimal.Parse(this.txtRateID.Text.Trim());
                    }
                    else
                    {
                        accessorialInfo.Rate = tempDec;
                    }
                }
                else
                {
                    accessorialInfo.Rate = tempDec;
                }
                accessorialInfo.CostType = this.ddlCostType.SelectedValue;
                accessorialInfo.ChargeInvocation = this.txtChargeInvocation.Text;
                accessorialInfo.CostInvocation = this.txtCostInvocation.Text;
                accessorialInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                accessorialInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(accessorialInfo, "ACCESSORIAL") + base.GetGlobalMsgResource("F9170_005");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9170_006") + ex.Message;
            }
        }
    }
}

